package com.newsystem.service.impl;

import com.newsystem.MyUtils.CommonResult;
import com.newsystem.MyUtils.MyUUIDUtils;
import com.newsystem.mapper.MediumMapper;
import com.newsystem.mapper.UserMapper;
import com.newsystem.pojo.Follow;
import com.newsystem.mapper.FollowMapper;
import com.newsystem.pojo.Medium;
import com.newsystem.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 我的收藏 服务实现类
 * </p>
 *
 * @author chenlitao
 * @since 2022-11-06
 */
@Service
public class FollowService extends ServiceImpl<FollowMapper, Follow> implements IFollowService {

    @Autowired
    private FollowMapper followMapper;
    @Autowired
    private MediumMapper mediumMapper;
    @Autowired
    private UserMapper userMapper;

    //todo 用户关注媒体，使用user的id
    public CommonResult<Object> instertFollow(String userId, String mediumId) {
        Medium medium = mediumMapper.selectByUserId(mediumId);
        if(medium == null || userMapper.selectById(mediumId).getStatue()==2){
            return CommonResult.failed("无此媒体号");
        }
        Follow follow = followMapper.selectFollow(userId,mediumId);
        if(follow != null){
            if(medium.getFansNum()!=null&&medium.getFansNum()>0) {
                medium.setFansNum(medium.getFansNum() - 1);
            }
            else {
                medium.setFansNum(0);
            }
            if(followMapper.deleteById(follow.getId())>0){
                mediumMapper.updateById(medium);
            }
            return CommonResult.success("取消关注");
        }
        else {
            if(medium.getFansNum() != null){
                medium.setFansNum(medium.getFansNum()+1);
            }
            else {
                medium.setFansNum(1);
            }
            follow = new Follow();
            follow.setId(MyUUIDUtils.getUUID());
            follow.setUserId(userId);
            follow.setMediumId(mediumId);
            if(followMapper.insert(follow)>0){
                mediumMapper.updateById(medium);
            }
            return CommonResult.success("成功关注");
        }
    }

    public CommonResult<Object> deleteFollow(String userId,String followId) {
        Follow follow = followMapper.selectFollowByUserFollow(userId,followId);
        if(follow==null){
            return CommonResult.failed("未有此关注");
        }
        Medium medium = mediumMapper.selectByUserId(follow.getMediumId());
        if(medium!=null){
            if(medium.getFansNum()!=null&&medium.getFansNum()>0){
                medium.setFansNum(medium.getFansNum()-1);
            }
            else {
                medium.setFansNum(0);
            }
            mediumMapper.updateById(medium);
        }
        if(followMapper.deleteById(followId)>0){
            return CommonResult.success("取消关注成功");
        }
        return CommonResult.failed("取消关注失败");
    }
}
